package com.authine.cloudpivot.web.api.secondopen.controller;

import com.alibaba.fastjson.JSON;
import com.authine.cloudpivot.web.api.controller.base.BaseController;
import com.authine.cloudpivot.web.api.secondopen.domain.AjaxResult;
import com.authine.cloudpivot.web.api.secondopen.service.BudgetImportService;
import com.authine.cloudpivot.web.api.secondopen.vo.BudgetVO;
import io.swagger.annotations.Api;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 预算导入控制器
 */
@Api(value = "二开接口", tags = "查询导入费用预算")
@RestController
@RequestMapping("/api/budgetImport")
public class BudgetImportController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(BudgetImportController.class);

    @Autowired
    BudgetImportService budgetImportService;

    /**
     * 根据年月成本中心查询费用类型
     */
    @RequestMapping("/queryBudget")
    public AjaxResult queryBudget(@RequestParam(value ="cbzx") String cbzx,
                                  @RequestParam(value = "nf") String nf,
                                  @RequestParam(value = "yf") String yf){
        log.info("获取参数:cbzx={},nf={},yf={}", cbzx,nf,yf);
        if(cbzx==null ||  cbzx==""){
            return AjaxResult.error(300,"查询失败，成本中心为空");
        }
        List<BudgetVO> budgetVOS = budgetImportService.queryBudget(cbzx, nf, yf);
        log.info("----------返回信息"+ JSON.toJSONString(budgetVOS));
        return AjaxResult.success(budgetVOS);
    }

    /**
     * 根据年月成本中心查询费用类型
     */
    @RequestMapping("/queryAndInsertBudget")
    public AjaxResult queryAndInsertBudget(@RequestParam(value ="cbzx") String cbzx,
                                  @RequestParam(value = "nf") String nf,
                                  @RequestParam(value = "yf") String yf){
        log.info("获取参数:cbzx={},nf={},yf={}", cbzx,nf,yf);
        if(cbzx==null ||  cbzx==""){
            return AjaxResult.error(300,"查询失败，成本中心为空");
        }
        List<BudgetVO> budgetVOS = budgetImportService.queryBudget(cbzx, nf, yf);
        if (CollectionUtils.isNotEmpty(budgetVOS)){
            int i = budgetImportService.insertBudget(budgetVOS);
        }
        log.info("----------返回信息"+ JSON.toJSONString(budgetVOS));
        return AjaxResult.success(budgetVOS);
    }







}
